package com.fowo.api.controller.purchase;


import com.alibaba.fastjson.JSON;
import com.fowo.api.common.model.R;
import com.fowo.api.entity.PurchaseChangeOrder;
import com.fowo.api.flow.entity.ProcessTemplates;
import com.fowo.api.flow.model.dto.StartProcessInstanceDTO;
import com.fowo.api.flow.model.vo.TemplateVo;
import com.fowo.api.flow.service.FlowService;
import com.fowo.api.flow.service.ProcessTemplateService;
import com.fowo.api.model.purchase.change.order.PurchaseChangeOrderVo;
import com.fowo.api.model.purchase.order.PurchaseOrderVo;
import com.fowo.api.service.PurchaseChangeOrderService;
import com.fowo.api.service.impl.PurchaseChangeOrderServiceImpl;
import com.fowo.api.service.impl.PurchaseOrderServiceImpl;
import com.fowo.api.service.purchase.PurchaseChangeOrderDService;
import com.fowo.api.user.model.CurrentUser;
import com.fowo.api.user.model.JwtUserInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Tag(name = "变更单")
@RequestMapping("/api/customized-purchase-change-order")
@RestController
public class PurchaseChangeOrderDController {
    @Resource
    private PurchaseOrderServiceImpl purchaseOrderService;
    @Resource
    protected PurchaseChangeOrderDService purchaseChangeOrderDService;


    @Operation(summary = "获取详情")
    @GetMapping("{id}")
    public R<PurchaseOrderVo> getVoById(@PathVariable Long id) {
        return R.data(() -> purchaseOrderService.getVoById(id));
    }
    @Operation(summary = "根据表单名称查询模版")
    @GetMapping("/query/table")
    public R <List<TemplateVo>>queryByTableName(@RequestParam(value = "tableName", required = false)String tableName,@RequestParam(value = "recordId", required = false)String recordId) throws Exception {
//        List<TemplateVo> templateVos = processTemplateService.queryByTableName(tableName);
//        JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
//        TemplateVo templateVo = templateVos.get(0);
//        Assert.notNull(templateVo,"没有配置审批流程");
//        //查询模板详情
//        ProcessTemplates detail = flowService.detail(templateVo.getTemplateId());
//        List<StartProcessInstanceDTO> startList=new ArrayList<>();
//        StartProcessInstanceDTO StartProcessInstanceDTO=new StartProcessInstanceDTO();
//        PurchaseChangeOrder byId = PurchaseChangeOrderService.getById(recordId);
//        StartProcessInstanceDTO.setFormData(JSON.parseObject(String.valueOf(byId)));
//        StartProcessInstanceDTO.setTemplateId(templateVo.getTemplateId());
//        StartProcessInstanceDTO.setProcessDefinitionId(detail.getProcessDefinitionId());
//        StartProcessInstanceDTO.setRecordId(Long.valueOf(recordId));
//        StartProcessInstanceDTO.setFormName(templateVo.getFormName());
//
//        CurrentUser currentUser=new CurrentUser();
//        currentUser.setUserid(String.valueOf(jwtUserInfo.getUserId()));
//        StartProcessInstanceDTO.setStartUserInfo(currentUser);
//        startList.add(StartProcessInstanceDTO);
//        flowService.start(startList);

        return R.data(null);
    }
    @PreAuthorize("@am.canAccess('purchaseOrderProcurementChangeOrderControl')")
    @Operation(summary = "新增")
    @PostMapping
    public R<Long> create(@Validated @RequestBody PurchaseChangeOrderVo model) {
        Assert.notNull(model.getOrderSn(),"采购单号不能为空");
        model.getProductItem().forEach(x->x.setBusinessIdent(null));
        return R.data(() -> purchaseChangeOrderDService.create(model));
    }
}
